package com.orangelife.service.propFeeCommInfo.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import net.sf.json.JSONObject;

import org.apache.commons.lang.StringUtils;

import com.orangelife.exception.AppBizException;
import com.orangelife.service.propFeeApartment.IPropFeeApartmentService;
import com.orangelife.service.propFeeApartment.model.PropFeeApartmentDto;
import com.orangelife.setting.constant.TypeConst;

public class HandleApartmentsThread extends Thread implements Runnable {

	private String threadName;
	private List<Map<String, Object>> data;
	private int start;
	private int end;

	private IPropFeeApartmentService propFeeApartmentService;

	public HandleApartmentsThread(String threadName,
			List<Map<String, Object>> data,
			IPropFeeApartmentService propFeeApartmentService) {
		this.threadName = threadName;
		this.data = data;
		this.propFeeApartmentService = propFeeApartmentService;
	}

	public void run() {
		try {

			/***
			 * 批量查询房屋
			 */
			List<String> apartmentIDs = new ArrayList<String>();
			List<PropFeeApartmentDto> apartmentInfos = new ArrayList<PropFeeApartmentDto>();
			for (Map<String, Object> apartment : data) {
				if (!StringUtils.isBlank(apartment.get("apartmentID")
						.toString())) {
					apartmentIDs.add(apartment.get("apartmentID").toString());
				}
			}

			if (apartmentIDs != null && apartmentIDs.size() > 0) {
				apartmentInfos = propFeeApartmentService
						.bauthfindPropFeeApartment(apartmentIDs);
			}

			List<PropFeeApartmentDto> apartments = new ArrayList<PropFeeApartmentDto>();
			for (Map<String, Object> apartment : data) {
				System.out.println(threadName + "\t\t\t\t\t 请求 数据" + "\t\t\t\t"
						+ JSONObject.fromObject(apartment).toString());
				PropFeeApartmentDto feeApartment = null;

				/***
				 * 判断房屋是否存在
				 */
				for (PropFeeApartmentDto propFeeApartmentDto : apartmentInfos) {
					if (propFeeApartmentDto.getApartmentID().equals(
							apartment.get("apartmentID").toString())) {
						feeApartment = propFeeApartmentDto;
						break;
					}
				}
				/***
				 * 房屋存在更新
				 */
				if (feeApartment != null) {
					feeApartment
							.setFloor(apartment.get("floor") != null ? apartment
									.get("floor").toString() : null);
					feeApartment
							.setBuildingArea(apartment.get("buildingArea") != null ? apartment
									.get("buildingArea").toString() : null);
					feeApartment
							.setIsDeliver(apartment.get("isDeliver") != null ? apartment
									.get("isDeliver").toString() : null);
					feeApartment
							.setPropertyArea(apartment.get("propertyArea") != null ? apartment
									.get("propertyArea").toString() : null);
					feeApartment
							.setIsOccupy(apartment.get("isOccupy") != null ? apartment
									.get("isOccupy").toString() : null);
					feeApartment.setApartmentNum(apartment.get("apartmentNumb")
							.toString());
					feeApartment.setBuildingID(apartment.get("buildingID")
							.toString());
					feeApartment
							.setUnitID(apartment.get("unitID") != null ? apartment
									.get("unitID").toString() : null);
					feeApartment.setApartmentType(apartment
							.get("apartmentType") != null ? apartment.get(
							"apartmentType").toString() : null);
					feeApartment
							.setFeeArea((apartment.get("feeArea") != null ? apartment
									.get("feeArea").toString() : null));
					feeApartment
							.setSourceID((apartment.get("sourceID") != null ? apartment
									.get("sourceID").toString() : null));
					feeApartment
							.setOtherName((apartment.get("otherName") != null ? apartment
									.get("otherName").toString() : null));

					if (apartment.get("joinTime") != null) {
						String jsonTime = apartment.get("joinTime").toString();
						if (!StringUtils.isBlank(jsonTime)
								&& !"null".equals(jsonTime)) {

							feeApartment.setJoinTime(new Date(Long
									.parseLong(jsonTime)));
						}
					}

					// feeApartment.setTsedit(new Date());
					propFeeApartmentService.editPropFeeApartment(feeApartment);
				} else {
					/***
					 * 房屋不存在添加
					 */
					List<PropFeeApartmentDto> propFeeApartment = propFeeApartmentService
							.findPropFeeApartmentByRelevantID(
									apartment.get("apartmentID").toString(),
									apartment.get("sourceID") != null ? apartment
											.get("sourceID").toString() : null,
									apartment.get("buildingID").toString(),
									apartment.get("unitID") != null ? apartment
											.get("unitID").toString() : null,
									null,
									apartment.get("apartmentType") != null ? apartment
											.get("apartmentType").toString()
											: null,
									apartment.get("apartmentNumb").toString());
					if (propFeeApartment == null
							|| propFeeApartment.size() <= 0) {
						PropFeeApartmentDto apartmentInfo = new PropFeeApartmentDto();
						apartmentInfo.setApartmentID(apartment.get(
								"apartmentID").toString());
						apartmentInfo
								.setFloor(apartment.get("floor") != null ? apartment
										.get("floor").toString() : null);
						apartmentInfo.setBuildingArea(apartment
								.get("buildingArea") != null ? apartment.get(
								"buildingArea").toString() : null);
						apartmentInfo
								.setIsDeliver(apartment.get("isDeliver") != null ? apartment
										.get("isDeliver").toString() : null);
						apartmentInfo.setPropertyArea(apartment
								.get("propertyArea") != null ? apartment.get(
								"propertyArea").toString() : null);
						apartmentInfo
								.setIsOccupy(apartment.get("isOccupy") != null ? apartment
										.get("isOccupy").toString() : null);
						apartmentInfo.setApartmentNum(apartment.get(
								"apartmentNumb").toString());
						apartmentInfo.setBuildingID(apartment.get("buildingID")
								.toString());
						apartmentInfo
								.setUnitID(apartment.get("unitID") != null ? apartment
										.get("unitID").toString() : null);
						apartmentInfo.setApartmentType(apartment
								.get("apartmentType") != null ? apartment.get(
								"apartmentType").toString() : null);
						apartmentInfo
								.setFeeArea((apartment.get("feeArea") != null ? apartment
										.get("feeArea").toString() : null));
						apartmentInfo
								.setSourceID((apartment.get("sourceID") != null ? apartment
										.get("sourceID").toString() : null));
						apartmentInfo
								.setOtherName((apartment.get("otherName") != null ? apartment
										.get("otherName").toString() : null));
						if (apartment.get("jsonTime") != null) {
							String jsonTime = apartment.get("jsonTime")
									.toString();
							if (!StringUtils.isBlank(jsonTime)) {
								JSONObject json = JSONObject
										.fromObject(jsonTime);
								apartmentInfo.setJoinTime(new Date(Long
										.parseLong(json.getString("time"))));
							}
						}
						apartmentInfo.setItemStat(TypeConst.FLAG_ON);
						apartmentInfo.setTscreate(new Date());
						apartmentInfo.setTsedit(new Date());
						apartments.add(apartmentInfo);
					}
				}

			}
			propFeeApartmentService.addPropFeeApartment(apartments);

		} catch (AppBizException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
